﻿package mortal.game.view.mail.view
{
    import Message.Game.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class MailWritePanel extends GSprite
    {
        private var _bg:ScaleBitmap;
        private var _txtName1:GTextFiled;
        private var _txtName2:GTextInput;
        private var _txtTitle1:GTextFiled;
        private var _txtTitle2:GTextInput;
        private var _friendCombox:GComboBox;
        private var _txtContent:MTextAreaBox;
        private var _txtStaticCost:GTextFiled;
        private var _txtCoin:GTextFiled;
        private var _bmpTongqian:GBitmap;
        private var _btnSend:GButton;

        public function MailWritePanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.bg(0, 0, 311, 290, this);
            this._txtName1 = UIFactory.textField("收件人：", 6, 5, 54, 20, this, null, false, false);
            this._txtName1.textColor = 16777215;
            this._txtTitle1 = UIFactory.textField("标   题：", 6, 28, 54, 20, this, null, false, false);
            this._txtTitle1.textColor = 16777215;
            this._friendCombox = UIFactory.gComboBox(173, (this._txtName1.y - 1), 130, 22, null, this);
            this.initFriendComBox();
            this._txtName2 = UIFactory.gTextInput(this._txtName1.x + this._txtName1.width, (this._txtName1.y - 1), 112, 22, this);
            this._txtName2.maxChars = 10;
            this._txtTitle2 = UIFactory.gTextInput(this._txtTitle1.x + this._txtTitle1.width, this._txtName2.y + 24 - 1, 244, 22, this);
            this._txtTitle2.maxChars = 10;
            this._txtContent = this.createTextAreaBox(this._txtName1.x + 3, 56, 293, 200);
            this._txtContent.ta.text = "";
            this._txtContent.ta.maxChars = 100;
            this._txtStaticCost = UIFactory.textField("本次邮资：", 10, 262, 60, 20, this);
            this._txtCoin = UIFactory.textField("100", 70, 262, 40, 20, this);
            this._txtCoin.textColor = GlobalStyle.colorHuangUint;
            this._bmpTongqian = UIFactory.bitmap(ImagesConst.Jinbi_bind, 100, (this._txtCoin.y + 1), this);
            this._btnSend = UIFactory.gButton("发送邮件", 230, 263, 70, 22, this);
            this._btnSend.configEventListener(MouseEvent.CLICK, this.btnSendClickHandler);
            this.setSendAble();
            GameProxy.friend.getFriendList(EFriendFlag._EFriendFlagFriend);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onLevelUpdate);
            return;
        }// end function

        private function onLevelUpdate(param1:Object) : void
        {
            this.setSendAble();
            return;
        }// end function

        public function setSendAble() : void
        {
            var _loc_1:* = GameDefConfig.instance.getSystemOpenLevel(87);
            if (Cache.instance.role.roleInfo.level < _loc_1)
            {
                this._btnSend.mouseEnabled = false;
                this._btnSend.filters = [FilterConst.colorFilter2];
            }
            else
            {
                this._btnSend.mouseEnabled = true;
                this._btnSend.filters = [];
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._bg.dispose(param1);
            this._txtName1.dispose(param1);
            this._txtTitle1.dispose(param1);
            this._friendCombox.dispose(param1);
            this._txtName2.dispose(param1);
            this._txtTitle2.dispose(param1);
            this._txtContent.dispose(param1);
            this._txtCoin.dispose(param1);
            this._txtStaticCost.dispose(param1);
            this._bmpTongqian.dispose(param1);
            this._btnSend.dispose(param1);
            this._bg = null;
            this._txtName1 = null;
            this._txtTitle1 = null;
            this._friendCombox = null;
            this._txtName2 = null;
            this._txtTitle2 = null;
            this._txtContent = null;
            this._txtCoin = null;
            this._txtStaticCost = null;
            this._bmpTongqian = null;
            this._btnSend = null;
            NetDispatcher.removeCmdListener(ServerCommand.RoleLevelUpdate, this.onLevelUpdate);
            return;
        }// end function

        private function initFriendComBox() : void
        {
            var _loc_3:SFriendRecord = null;
            this._friendCombox.addItem({label:"选择好友", type:0});
            var _loc_1:* = Cache.instance.friend.getCacheDataByType(EFriendFlag._EFriendFlagFriend);
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                _loc_3 = _loc_1[_loc_2];
                this._friendCombox.addItem({label:_loc_3.friendPlayer.name, type:1});
                _loc_2++;
            }
            this._friendCombox.configEventListener(Event.CHANGE, this.onFriendComboboxChange);
            return;
        }// end function

        private function onFriendComboboxChange(event:Event) : void
        {
            var _loc_2:* = this._friendCombox.selectedItem;
            if (_loc_2.type == 1)
            {
                this._txtName2.text = _loc_2.label;
            }
            return;
        }// end function

        private function createTextAreaBox(param1:int, param2:int, param3:int = 117, param4:int = 23) : MTextAreaBox
        {
            var _loc_5:MTextAreaBox = null;
            _loc_5 = UICompomentPool.getUICompoment(MTextAreaBox);
            _loc_5.createDisposedChildren();
            _loc_5.bg.width = param3;
            _loc_5.ta.width = param3;
            _loc_5.bg.height = param4;
            _loc_5.ta.height = param4;
            _loc_5.ta.editable = true;
            _loc_5.x = param1;
            _loc_5.y = param2;
            this.addChild(_loc_5);
            return _loc_5;
        }// end function

        public function setToPlyerName(param1:String) : void
        {
            if (param1)
            {
                this._txtName2.text = param1;
            }
            return;
        }// end function

        private function btnSendClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = GameDefConfig.instance.getSystemOpenLevel(87);
            if (Cache.instance.role.roleInfo.level < _loc_2)
            {
                MsgManager.showRollTipsMsg("必须" + String(_loc_2) + "级才能发送邮件");
                return;
            }
            if (StringHelper.isWhitespace(this._txtName2.text))
            {
                MsgManager.showRollTipsMsg("请输入收件人");
                return;
            }
            if (this._txtName2.text == Cache.instance.role.playerInfo.name)
            {
                MsgManager.showRollTipsMsg("不能给自己发邮件");
                return;
            }
            if (Cache.instance.friend.isBlackListByName(this._txtName2.text))
            {
                MsgManager.showRollTipsMsg("不能给黑名单玩家发邮件");
                return;
            }
            if (StringHelper.isWhitespace(this._txtTitle2.text))
            {
                MsgManager.showRollTipsMsg("请输入主题");
                return;
            }
            if (StringHelper.isWhitespace(this._txtContent.ta.text))
            {
                MsgManager.showRollTipsMsg("请输入内容");
                return;
            }
            if (Cache.instance.role.money.coinBind < 100 && Cache.instance.role.money.coin < 100)
            {
                MsgManager.showRollTipsMsg("发送失败，金币不足");
                return;
            }
            var _loc_3:Object = {};
            _loc_3.toPlayerName = this._txtName2.text;
            _loc_3.title = this._txtTitle2.text;
            _loc_3.content = this._txtContent.ta.text;
            Dispatcher.dispatchEvent(new DataEvent(EventName.MailSend, _loc_3));
            return;
        }// end function

        public function clearWord() : void
        {
            this._txtName2.text = "";
            this._txtTitle2.text = "";
            this._txtContent.ta.text = "";
            return;
        }// end function

    }
}
